const jimp = require('jimp');//jimp is a library for image manipulation
const Multer = require('koa-multer');
const path = require("path");
const avatarUpload = Multer({
  dest: './public/uploads/avatar'
});
const pictureUpload = Multer({
  dest: './public/uploads/picture'
});
const avatarHandler = avatarUpload.single('avatar');  //avatar is the name of the input field
const pictureHandler = pictureUpload.array('picture',9);  //picture is the name of the input field

//设置多张图片大小
const pictureResize =async (ctx,next) => {
  const files = ctx.req.files
for(let file of files) {
  const destPath = path.join(file.destination, file.filename);  //获取文件的绝对路径
  jimp.read(file.path, (err, image) => {
    if (err) throw err;
    //保存不同尺寸的图片
    image.resize(1280, jimp.AUTO).write(`${destPath}-large`);  //200x200
    image.resize(640, jimp.AUTO).write(`${destPath}-medium`); //100x100
    image.resize(320, jimp.AUTO).write(`${destPath}-small`); //50x50
  });

}
await next();
}
module.exports = {
  avatarHandler,
  pictureHandler,
  pictureResize
}